package com.guinea.account.service;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

import com.guinea.account.dao.RoleDao;
import com.guinea.account.entity.Menu;
import com.guinea.account.entity.Resource;
import com.guinea.account.entity.Role;
import com.guinea.account.entity.User;
import com.guinea.core.acegi.cache.AcegiCacheManager;
import com.guinea.core.orm.hibernate.HibernateDao;
import com.guinea.core.service.CRUDManager;

/**
 * @author huaxiaz
 * @see HibernateEntityDao
 * @see org.springside.core.dao.HibernateGenericDao
 */
public class RoleManager extends CRUDManager{
	private static final String QUERY_USER_BY_ROLEID = "select u from User u left join u.roles r where r.id=?";

	private static final String QUERY_RESOURCE_BY_ROLEID = "select res from Resource res left join res.roles r where r.id=?";

	private static final String QUERY_MENU_BY_ROLEID = "select m from Menu m left join m.roles r where r.id=?";
	private AcegiCacheManager acegiCacheManager;


	private RoleDao roleDao;

	public HibernateDao getEntityDao() {
		return getRoleDao();
	}

	public Role getRole(Serializable id) {
		return (Role)getRoleDao().get(id);
	}

	public RoleDao getRoleDao() {
		return roleDao;
	}

	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}
	
	public void setAcegiCacheManager(AcegiCacheManager acegiCacheManager) {
		this.acegiCacheManager = acegiCacheManager;
	}
	public List getUsersByRoleId(Serializable roleId) {
		return getEntityDao().createQuery(QUERY_USER_BY_ROLEID, roleId).list();
	}
	public List getResourcesByRoleId(Serializable roleId) {
		return getEntityDao().createQuery(QUERY_RESOURCE_BY_ROLEID, roleId).list();
	}
	public List getMenusByRoleId(Serializable roleId) {
		return getEntityDao().createQuery(QUERY_MENU_BY_ROLEID, roleId).list();
	}
	//标记删除
	public void eraseRole(Role role){
		getEntityDao().save(role);
		handleRefrence(role);
		acegiCacheManager.refreshCaches();
	}
	public void deleteById(Serializable id) {
		Role role = (Role) get(id);
		handleRefrence(role);
		getEntityDao().delete(role);
		acegiCacheManager.refreshCaches();
	}
	private void handleRefrence(Role role){
		Long roleId = role.getId();
		// 查询出拥有该角色的用户,并删除关联.
		List users = getUsersByRoleId(roleId);
		for (Iterator iter = users.iterator(); iter.hasNext();) {
			User user = (User) iter.next();
			user.getRoles().remove(role);
		}
		// 查询出拥有该角色的资源,并删除关联.
		List resources = getResourcesByRoleId(roleId);
		for (Iterator iter = resources.iterator(); iter.hasNext();) {
			Resource resource = (Resource) iter.next();
			resource.getRoles().remove(role);
		}
		// 查询出拥有该角色的菜单,并删除关联.
		List menus = getMenusByRoleId(roleId);
		for (Iterator iter = menus.iterator(); iter.hasNext();) {
			Menu menu = (Menu) iter.next();
			menu.getRoles().remove(role);
		}
	}
	
	public List getRolesByName(String name){
		return getRoleDao().createQuery("select r from Role r where r.status = '1' and r.roleName = ?",name).list();
	}

}
